package com.haieros.www.purerunshowsystem.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;

import com.haieros.www.purerunshowsystem.GridViewLocalVideoActivity;
import com.haieros.www.purerunshowsystem.MusicModel;
import com.haieros.www.purerunshowsystem.R;
import com.haieros.www.purerunshowsystem.VideoModel;
import com.haieros.www.purerunshowsystem.adapter.MyMusicAdapter;
import com.haieros.www.purerunshowsystem.service.MusicService;
import com.haieros.www.purerunshowsystem.utils.SDUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/24 0024.
 */

public class MusicActivity extends AppCompatActivity {
    private ArrayList<String> mListStrUrl = new ArrayList<>();
    private RecyclerView mRecycler;
    private List<MusicModel> mListModel = new ArrayList<>();
    private static String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myTinkerDemo";
    private MyMusicAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        initView();
        //initFile();
        MusicActivity.ScannerAnsyTask ansyTask=new MusicActivity.ScannerAnsyTask();
        ansyTask.execute();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        Log.e("TAG", "dispatchKeyEvent: Activity 传递事件");
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mRecycler.requestFocus();
        mRecycler.setLayoutManager(new LinearLayoutManager(this));

//        mAdapter.notifyDataSetChanged();
    }

    private void initFile() {
//        File file = new File("/data/music");
        File file = new File(filePath);
        File[] files = file.listFiles();
        if (files != null){
            Log.e("TAG", "initFile: "+files.length);
            for (int i = 0; i < files.length; i++) {
                String fileName = files[i].getName();
                int lastOf = fileName.lastIndexOf(".");
                if (lastOf != -1){
                    String name = fileName.substring(lastOf);
                    if (name.equalsIgnoreCase(".mp3")){
//                        mListModel.add(new MusicModel(files[i].getPath(),files[i].getName(),""));
                        try {
                            mListModel.add(new MusicModel(files[i].getPath(),files[i].getName(),""));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    Log.e("TAG", "initFile: " + files[i].getPath()+"---"+files[i].getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    //在主线程更新界面
    private Handler handler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    mRecycler.setAdapter(mAdapter);
                    //gridView.setAdapter(mAdapter);
                    break;
            }
            return false;
        }
    });
    //异步执行
    public class ScannerAnsyTask extends AsyncTask<Void,Integer,List<MusicModel>> {
        private List<MusicModel> videoInfos = new ArrayList<MusicModel>();

        @Override
        protected List<MusicModel> doInBackground(Void... params) {
            videoInfos = SDUtils.getMusicFile(videoInfos,null);
            Log.i("tga", "最后的大小" + videoInfos.size());
            return videoInfos;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(List<MusicModel> videoInfos1) {
            super.onPostExecute(videoInfos1);
            for (int i = 0; i < videoInfos1.size(); i++) {
                mListStrUrl.add(videoInfos1.get(i).getPath());
            }

            mAdapter = new MyMusicAdapter(getApplicationContext(),videoInfos1,new Intent(getApplicationContext(), MusicService.class));
            Message msg = new Message();
            msg.what = 1;
            handler.sendMessage(msg);
        }
    }
    private void initView() {
        mRecycler = findViewById(R.id.music_recycler);
    }
}
